package com.wbao.dianniu.ui;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.sdk.android.media.upload.UploadListener;
import com.alibaba.sdk.android.media.upload.UploadTask;
import com.alibaba.sdk.android.media.utils.FailReason;
import com.alibaba.sdk.android.media.utils.StringUtils;
import com.arlen.photo.photopickup.presenter.HeadPresenter;
import com.arlen.photo.photopickup.util.DensityUtils;
import com.arlen.photo.photopickup.util.FileSizeUtil;
import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.OptionsPickerView.OnOptionsSelectListener;
import com.bumptech.glide.Glide;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.customView.LoadingWaitDialog;
import com.wbao.dianniu.customView.NoDoubleClickListener;
import com.wbao.dianniu.data.ChildZone;
import com.wbao.dianniu.data.HeadInfoData;
import com.wbao.dianniu.data.Zone;
import com.wbao.dianniu.emojiUtils.SharedPreferencedUtils;
import com.wbao.dianniu.listener.IAccountModifyListener;
import com.wbao.dianniu.listener.IHeadInfoListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AccountModifyManager;
import com.wbao.dianniu.update.HeadImageManger;
import com.wbao.dianniu.manager.HeadInfoManager;
import com.wbao.dianniu.utils.GlideCircleTransform;
import com.wbao.dianniu.utils.JsonUtil;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.utils.PermissionUtils;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 资料
 *
 * @author yejianjun
 */
public class PerfectInfoActivity extends BaseActivity implements OnClickListener, IHeadInfoListener, IAccountModifyListener {
    private final int INDUSTRY_CODE = 1000;
    private final int PLATFORM_CODE = 2000;
    private final int JOB_CODE = 3000;
    private static final int NICE_REQ_CODE = 10010;
    private static final int SIGNATE_REQ_CODE = 10011;
    private static final int SEX_REQ_CODE = 10013;
    private final int IMAGE_OPEN = 1; // 打开图片标记


    private LinearLayout waittingLayout;
    private ProgressBar waittingProBar;
    private TextView waittingTV;
    private RelativeLayout headRL;
    private ImageView headIV;
    private RelativeLayout nicknameRL;
    private TextView nicknameTV;
    private RelativeLayout sexRL;
    private TextView sexTV;
    private RelativeLayout signateRL;
    private TextView signateTV;
    private Dialog dialog;
    private Button photoBtn;
    private Button choosePicBtn;
    private Button cancel_dialog;
    private AccountModifyManager modifyManger;
    private RelativeLayout industryRL;
    private RelativeLayout jobsRL;
    private RelativeLayout platformRL;
    private RelativeLayout regionRL;
    private TextView industryTV;
    private TextView jobsTV;
    private TextView platformTV;
    private TextView areaTV;
    private Button rightBtn;
//	int selectFlag = -1;
    /**
     * 省列表
     */
    private LinkedList<ChildZone> province_list = new LinkedList<ChildZone>();
    /**
     * 市列表
     */
    private ArrayList<LinkedList<ChildZone>> city_list = new ArrayList<>();
    /**
     * 省数据
     */
    private ArrayList<String> provinceList = new ArrayList<>();
    /**
     * 市数据
     */
    private ArrayList<ArrayList<String>> cityList = new ArrayList<>();
    private OptionsPickerView pvOptions;
    private int provinceId = 0;
    private int cityId = 0;
    private String province;
    private String city;
    private String headBitmap = null;
    private String headPic = null;
    private Dialog pDialog;
    //	private boolean isChangeFlag = false;
    private HeadInfoData historyData = null;
    private boolean isHeadPicChange = false;
    private HeadImageManger headImageManger;
    private HeadInfoManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_perfect_info,
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.edit_information));
        initWattingView();
        // 请求数据
        requestData(GlobalContext.getAccountId(), GlobalContext.getAccountId());
        initView();
        rightBtn = getRightTextButton();
        rightBtn.setText(getResources().getString(R.string.save));
        rightBtn.setOnClickListener(noDoubleClickListener);
        initAreaData();
    }

    private void initWattingView() {
        waittingLayout = findViewById(R.id.waitting_layout);
        waittingProBar = findViewById(R.id.waitting_progress);
        waittingTV = findViewById(R.id.waitting_text_desc);
    }

    private void initView() {
        headRL = findViewById(R.id.perfect_head_rl);
        nicknameRL = findViewById(R.id.perfect_nickname_rl);
        sexRL = findViewById(R.id.perfect_sex_rl);
        signateRL = findViewById(R.id.perfect_signate_rl);
        industryRL = findViewById(R.id.perfect_industry_rl);
        jobsRL = findViewById(R.id.perfect_jobs_rl);
        platformRL = findViewById(R.id.perfect_platform_rl);
        regionRL = findViewById(R.id.perfect_region_rl);
        headRL.setOnClickListener(this);
        nicknameRL.setOnClickListener(this);
        sexRL.setOnClickListener(this);
        signateRL.setOnClickListener(this);
        industryRL.setOnClickListener(this);
        jobsRL.setOnClickListener(this);
        platformRL.setOnClickListener(this);
        regionRL.setOnClickListener(this);

        headIV = findViewById(R.id.perfect_head_iv);
        PictureDownloadUtils.displayHeadImage(this, headIV, GlobalContext.getHeadPic());
        nicknameTV = findViewById(R.id.perfect_nickname_tv);
        sexTV = findViewById(R.id.perfect_sex_tv);
        signateTV = findViewById(R.id.perfect_signate_tv);
        industryTV = findViewById(R.id.perfect_industry_tv);
        jobsTV = findViewById(R.id.perfect_jobs_tv);
        platformTV = findViewById(R.id.perfect_platform_tv);
        areaTV = findViewById(R.id.perfect_area_tv);
    }

    /**
     * 地区选择器
     */
    private void initAreaData() {
        //选项选择器
        pvOptions = new OptionsPickerView(this);
        InputStream input = getResources().openRawResource(R.raw.zone);
        String jsonData = Utils.inputStream2String(input, "utf-8");
        List<Zone> zoneList = JsonUtil.toObjectList(jsonData, Zone.class);
        for (Iterator iterator = zoneList.iterator(); iterator.hasNext(); ) {
            Zone zone = (Zone) iterator.next();
            ChildZone child = new ChildZone();
            child.setId(zone.getId());
            child.setName(zone.getName());
            province_list.add(child);
            provinceList.add(zone.getName());
            LinkedList<ChildZone> linkList = zone.getChildrens();
            city_list.add(linkList);
            ArrayList<String> arrayList = new ArrayList<String>();
            for (int i = 0; i < linkList.size(); i++) {
                arrayList.add(linkList.get(i).getName());
            }
            cityList.add(arrayList);
        }

        pvOptions.setPicker(provinceList, cityList, true);
        pvOptions.setTitle(getResources().getString(R.string.city_selected));
        pvOptions.setCyclic(false);

        pvOptions.setSelectOptions(provinceId, cityId);
        pvOptions.setOnoptionsSelectListener(new OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                if (option2 >= cityList.get(options1).size()) { // 快速滑动时，最后一个超过界限，则显示最后一个
                    option2 = cityList.get(options1).size() - 1;
                }
                provinceId = options1;
                cityId = option2;
                province = provinceList.get(options1);
                city = cityList.get(options1).get(option2);
                String areaStr = provinceList.get(options1) + cityList.get(options1).get(option2);
                areaTV.setText(areaStr);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_button:
                if (isNoChanged()) {
                    finish();
                    break;
                }
                CustomDialog.Builder builder = new CustomDialog.Builder(this);
                builder.setTitle("确认放弃本次资料编辑吗？");
                builder.setPositiveButton(getResources().getString(R.string.confirm),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                finish();
                            }
                        });
                builder.setNegativeButton(getResources().getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
                break;
            /*case R.id.right_button:    //保存
                if (checkNull()) {
                    Notification.toast(this, "必填项不能为空");
                    return;
                } else {
                    Integer sex = null;
                    String sexStr = sexTV.getText().toString();
                    if (sexStr.equals("女")) {
                        sex = 0;
                    } else if (sexStr.equals("男")) {
                        sex = 1;
                    }
                    modifyManger.modify(GlobalContext.getAccountId(), headPic, nicknameTV.getText().toString().trim(), sex, signateTV.getText().toString().trim(), industryTV.getText().toString(), jobsTV.getText().toString(), platformTV.getText().toString(), province, city);
                    // 提示等待框

                }
                break;*/
            case R.id.perfect_head_rl:    //修改头像
                showRealase();
                break;
            case R.id.perfect_nickname_rl:    //编辑昵称
                Intent niceIntent = new Intent();
                niceIntent.putExtra(Const.INTENT_NICENAME, nicknameTV.getText().toString().trim());
                niceIntent.setClass(PerfectInfoActivity.this, NiceNameEdit.class);
                startActivityForResult(niceIntent, NICE_REQ_CODE);
                break;
            case R.id.perfect_sex_rl:
                Intent sexIntent = new Intent();
                String sexText = sexTV.getText().toString().trim();
                Integer sexFlag = -1;
                if (!TextUtils.isEmpty(sexText)) {
                    if (sexText.equals("男")) {
                        sexFlag = 1;
                    } else if (sexText.equals("女")) {
                        sexFlag = 0;
                    }
                }
                sexIntent.putExtra(Const.INTENT_SEX, sexFlag);
                sexIntent.setClass(PerfectInfoActivity.this, SexEdit.class);
                startActivityForResult(sexIntent, SEX_REQ_CODE);
                break;
            case R.id.perfect_signate_rl:
                Intent signateIntent = new Intent();
                signateIntent.putExtra(Const.INTENT_SIGNATE, signateTV.getText().toString().trim());
                signateIntent.setClass(PerfectInfoActivity.this, SignateEdit.class);
                startActivityForResult(signateIntent, SIGNATE_REQ_CODE);
                break;

            case R.id.perfect_industry_rl: // 行业
                Intent industryIntent = new Intent();
                industryIntent.putExtra("industry", industryTV.getText().toString().trim());
                industryIntent.setClass(PerfectInfoActivity.this, TradeListActivity.class);
                startActivityForResult(industryIntent, INDUSTRY_CODE);
                break;
            case R.id.perfect_jobs_rl:    // 岗位
                Intent jobIntent = new Intent();
                jobIntent.putExtra("jobSelect", jobsTV.getText().toString().trim());
                jobIntent.setClass(PerfectInfoActivity.this, JobSelectActivity.class);
                startActivityForResult(jobIntent, JOB_CODE);
                break;
            case R.id.perfect_platform_rl:
                Intent platformIntent = new Intent();
                platformIntent.putExtra("platform", platformTV.getText().toString().trim());
                platformIntent.setClass(PerfectInfoActivity.this, PlatformListActivity.class);
                startActivityForResult(platformIntent, PLATFORM_CODE);
                break;
            case R.id.perfect_region_rl:
                pvOptions.show();
                break;
            case R.id.btn_cancel:
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                break;
            case R.id.btn_take_photo:
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                PermissionUtils.getInstance().intentToCamera(this);
                break;
            case R.id.btn_pick_photo:
                if (null != dialog && dialog.isShowing()) {
                    dialog.dismiss();
                }
                //选择图片
                PermissionUtils.getInstance().intentToPhoto(PerfectInfoActivity.this, IMAGE_OPEN, 1, null);
                break;
            default:
                break;
        }

    }

    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.right_button:    //保存
                    if (checkNull()) {
                        Notification.toast(PerfectInfoActivity.this, "必填项不能为空");
                        return;
                    } else {
                        Integer sex = null;
                        String sexStr = sexTV.getText().toString();
                        if (sexStr.equals("女")) {
                            sex = 0;
                        } else if (sexStr.equals("男")) {
                            sex = 1;
                        }
                        // 提示等待框
                        showLoadingDialog();
                        modifyManger.modify(GlobalContext.getAccountId(), headPic, nicknameTV.getText().toString().trim(), sex, signateTV.getText().toString().trim(), industryTV.getText().toString(), jobsTV.getText().toString(), platformTV.getText().toString(), province, city);
                    }
                    break;

                default:
                    break;
            }
        }
    };


    // 请求数据
    private void requestData(int accountId, int friendId) {
        manager = new HeadInfoManager(this);
        manager.addHeadListener(this);
        manager.headinfo(accountId, friendId);
        modifyManger = new AccountModifyManager(this);
        modifyManger.addAccountModifyListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		super.onActivityResult(requestCode, resultCode, data);
        //打开图片
        if (resultCode == RESULT_OK && requestCode == IMAGE_OPEN) {
            Uri uri = data.getData();
            if (!TextUtils.isEmpty(uri.getAuthority())) {
                //查询选择图片  
                Cursor cursor = getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA},
                        null, null, null);
                //返回 没找到选择图片  
                if (null == cursor) {
                    return;
                }
                //光标移动至开头 获取图片路径  
                cursor.moveToFirst();
                String pathImage = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                cursor.close();
                Intent pIntent = new Intent();
                pIntent.setClass(this, CutImageActivity.class);
                pIntent.putExtra(Const.BUNDLE_DATA, pathImage);
                startActivityForResult(pIntent, 110);
            }
        } else if (resultCode == RESULT_OK && requestCode == PermissionUtils.TAKE_PHOTO) {
            File file = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
            // 照片先缓存到本地
            BitmapFactory.Options options = FileSizeUtil.getBitmapOptions(file.getPath());
            int screenMax = Math.max(DensityUtils.getWindowWidth(this),
                    DensityUtils.getWindowHeight(this));
            int imgMax = Math.max(options.outWidth, options.outHeight);
            int inSimpleSize = 1;
            if (screenMax <= imgMax) {
                inSimpleSize = Math.max(screenMax, imgMax) / Math.min(screenMax, imgMax);
            }
            String cachPath = "image_" + StringUtils.getUUID();
            // 缓存图片到本地
            String compressPath = FileSizeUtil.compressBitmap(this,
                    file.getAbsolutePath(),
                    Bitmap.CompressFormat.JPEG,
                    options.outWidth / inSimpleSize,
                    options.outHeight / inSimpleSize,
                    false, cachPath);
            Intent pIntent = new Intent();
            pIntent.setClass(this, CutImageActivity.class);
            pIntent.putExtra(Const.BUNDLE_DATA, compressPath);
            startActivityForResult(pIntent, 110);
            /* //图片选择成功，上传图片到cdn，得到cdn的路径
             HeadPresenter presenter = new HeadPresenter(PerfectInfoActivity.this);
             presenter.addUploadListener(uploadListener);
            headPic = "image_" + StringUtils.getUUID();
            presenter.uploadImageUrl(picture.getAbsolutePath(), headPic);
            headBitmap = FileSizeUtil.compressBitmap(picture.getPath(), headIV.getWidth(), headIV.getHeight());
            headIV.setImageBitmap(headBitmap);
            isHeadPicChange = true;*/
        } else if (resultCode == RESULT_OK && requestCode == INDUSTRY_CODE) {
            String name = data.getStringExtra(Const.INTENT_MORE_TRADE);
            if (!TextUtils.isEmpty(name)) {
                industryTV.setText(name);
            } else {
                industryTV.setText("");
            }
        } else if (resultCode == RESULT_OK && requestCode == PLATFORM_CODE) {
            String name = data.getStringExtra(Const.INTENT_MORE_TRADE);
            if (!TextUtils.isEmpty(name)) {
                platformTV.setText(name);
            } else {
                platformTV.setText("");
            }
        } else if (resultCode == RESULT_OK && requestCode == JOB_CODE) {
            String name = data.getStringExtra(Const.INTENT_MORE_TRADE);
            if (!TextUtils.isEmpty(name)) {
                jobsTV.setText(name);
            } else {
                jobsTV.setText("");
            }
        } else if (resultCode == RESULT_OK && requestCode == NICE_REQ_CODE) {
            String name = data.getStringExtra(Const.NICE_NAME_INTENT);
            if (!TextUtils.isEmpty(name)) {
                nicknameTV.setText(name);
            } else {
                nicknameTV.setText("");
            }
        } else if (resultCode == RESULT_OK && requestCode == SEX_REQ_CODE) {
            int sex = data.getIntExtra(Const.INTENT_SEX, -1);
            if (0 == sex) {
                sexTV.setText("女");
            } else if (1 == sex) {
                sexTV.setText("男");
            } else {
                sexTV.setText("");
            }
        } else if (resultCode == RESULT_OK && requestCode == SIGNATE_REQ_CODE) {
            String name = data.getStringExtra(Const.INTENT_SIGNATE);
            if (!TextUtils.isEmpty(name)) {
                signateTV.setText(name);
            } else {
                signateTV.setText("");
            }
        } else if (requestCode == 110 && resultCode == RESULT_OK) {
            String cachePath = data.getStringExtra("cachePath");
            String uploadName = data.getStringExtra("headPic");
            if (null != cachePath && null != uploadName) {
                headPic = uploadName;
                showLoadingDialog();
                //图片选择成功，上传图片到cdn，得到cdn的路径
                HeadPresenter presenter = new HeadPresenter(PerfectInfoActivity.this);
                presenter.addUploadListener(uploadListener);
                presenter.uploadImageNotCompress(cachePath, uploadName);
//                 headBitmap = FileSizeUtil.compressBitmap(cachePath, headIV.getWidth(), headIV.getHeight());
                //BitmapFactory.decodeFile(cachePath);
//     			headIV.setImageBitmap(headBitmap);
                headBitmap = cachePath;
                Glide.with(this)
                        .load(new File(cachePath))
                        .centerCrop()
                        .placeholder(R.drawable.default_head_icon)
                        .crossFade()
                        .override(Utils.dip2px(this, 40), Utils.dip2px(this, 40))
                        .bitmapTransform(new GlideCircleTransform(this))
                        .thumbnail(0.1f)
                        .into(headIV);
                isHeadPicChange = true;
            }
        }
    }


    @Override
    public void onHeadFailure(int errorCode, String errorMsg) {
        waittingLayout.setVisibility(View.VISIBLE);
        waittingProBar.setVisibility(View.GONE);
        waittingTV.setText(errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onHeadSuccess(HeadInfoData objdata) {
        waittingLayout.setVisibility(View.GONE);
        if (null != objdata) {
            historyData = objdata;
            if (null == objdata.getHeadPic()) {
                //为空，默认加载一张图片
                headIV.setImageResource(R.drawable.default_head_icon);
            } else {
                PictureDownloadUtils.displayHeadImage(this, headIV, objdata.getHeadPic());
//				HeadPresenter presenter = new HeadPresenter(this);
//				presenter.showHeadPic(this, objdata.getHeadPic(), headIV);
            }
            headPic = objdata.getHeadPic();
            nicknameTV.setText(objdata.getRealName() == null ? "" : objdata.getRealName());
            if (null == objdata.getSex()) {
                sexTV.setText("");
            } else {
                int sexInt = Integer.valueOf(objdata.getSex());
                if (0 == sexInt) {
                    sexTV.setText("女");
                } else if (1 == sexInt) {
                    sexTV.setText("男");
                }
            }
            signateTV.setText(objdata.getDescribe() == null ? "" : objdata.getDescribe());
            industryTV.setText(objdata.getIndustry() == null ? "" : objdata.getIndustry());
            jobsTV.setText(objdata.getPost() == null ? "" : objdata.getPost());
            platformTV.setText(objdata.getPlatform() == null ? "" : objdata.getPlatform());
            province = objdata.getProvince();
            city = objdata.getCity();
            if (TextUtils.isEmpty(province)) {
                province = "";
            }
            if (TextUtils.isEmpty(city)) {
                city = "";
            }
            areaTV.setText(province + city);
        }
    }

    /**
     * 选择图片弹出框
     */
    private void showRealase() {
        dialog = new Dialog(this, R.style.custom_Dialog);
        try {
            Window win = dialog.getWindow();
            win.getDecorView().setPadding(0, 0, 0, 0);
            win.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
            win.setWindowAnimations(R.style.buttom_animation);
        } catch (Exception e) {
            e.printStackTrace();
        }
        View view = LayoutInflater.from(this).inflate(R.layout.picture_select, null);
        photoBtn = view.findViewById(R.id.btn_take_photo);
        choosePicBtn = view.findViewById(R.id.btn_pick_photo);
        cancel_dialog = view.findViewById(R.id.btn_cancel);
        photoBtn.setOnClickListener(this);
        choosePicBtn.setOnClickListener(this);
        cancel_dialog.setOnClickListener(this);
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.show();
    }

    private boolean checkNull() {
        if (TextUtils.isEmpty(nicknameTV.getText().toString().trim())) {
            return true;
        } else if (TextUtils.isEmpty(sexTV.getText().toString().trim())) {
            return true;
        } else if (TextUtils.isEmpty(industryTV.getText().toString().trim())) {
            return true;
        } else if (TextUtils.isEmpty(jobsTV.getText().toString().trim())) {
            return true;
        } else if (TextUtils.isEmpty(platformTV.getText().toString().trim())) {
            return true;
        } else if (TextUtils.isEmpty(areaTV.getText().toString().trim())) {
            return true;
        }
        return false;
    }

    @Override
    public void onModifyFailure(int errorCode, String errorMsg) {
        cancelLoadingDialog();
        Notification.toast(this, errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onModifySuccess() {
        cancelLoadingDialog();
        GlobalContext.setPerfectData(true);
        headImageManger = HeadImageManger.getInstance();
        if (null != headBitmap) {
            headImageManger.changeImageData(headBitmap);
            GlobalContext.setHeadPic(headPic);
        }
        if (null != nicknameTV.getText().toString().trim()) {
            GlobalContext.setNiceName(nicknameTV.getText().toString());
        }
        if (null != sexTV.getText().toString().trim()) {
            if ("女".equals(sexTV.getText().toString().trim())) {
                GlobalContext.setSex(0);
            } else if ("男".equals(sexTV.getText().toString().trim())) {
                GlobalContext.setSex(1);
            }
        }
        if (null != signateTV.getText().toString().trim()) {
            GlobalContext.setSignate(signateTV.getText().toString());
        }
        String label = industryTV.getText().toString() + "/" + jobsTV.getText().toString() + "/" + platformTV.getText().toString() + "|" + areaTV.getText().toString();
        if (null != label) {
            GlobalContext.setLabel(label);
        }
        headImageManger.changeDatas(nicknameTV.getText().toString().trim(), headPic, sexTV.getText().toString().trim(), signateTV.getText().toString().trim(), label);
//        Notification.toast(this, "已保存");
        finish();
    }

    /**
     * 上传图片事件监听
     */
    UploadListener uploadListener = new UploadListener() {

        @Override
        public void onUploading(UploadTask uploadTask) {
        }

        @Override
        public void onUploadFailed(UploadTask arg0, FailReason arg1) {
            // TODO Auto-generated method stub
            cancelLoadingDialog();
            Notification.toast(PerfectInfoActivity.this, "上传失败");
        }

        @Override
        public void onUploadComplete(UploadTask arg0) {
            cancelLoadingDialog();
        }

        @Override
        public void onUploadCancelled(UploadTask arg0) {
            cancelLoadingDialog();
        }
    };

    protected void onDestroy() {
        super.onDestroy();
        cancelLoadingDialog();
        if (null != modifyManger) {
            modifyManger.removeAccountModifyListener(this);
        }
        if (null != manager) {
            manager.removeHeadListener(this);
        }

    }

    private boolean isNoChanged() {
        if (null == historyData)
            return true;
        int sex = -1;
        String sexStr = sexTV.getText().toString().trim();
        if (sexStr.equals("女")) {
            sex = 0;
        } else if (sexStr.equals("男")) {
            sex = 1;
        }
        if (TextUtils.isEmpty(historyData.getRealName())) {
            historyData.setRealName("");
        }
        if (null == historyData.getSex()) {
            historyData.setSex(-1);
        }
        if (TextUtils.isEmpty(historyData.getDescribe())) {
            historyData.setDescribe("");
        }
        if (TextUtils.isEmpty(historyData.getIndustry())) {
            historyData.setIndustry("");
        }
        if (TextUtils.isEmpty(historyData.getPost())) {
            historyData.setPost("");
        }
        if (TextUtils.isEmpty(historyData.getPlatform())) {
            historyData.setPlatform("");
        }
        if (TextUtils.isEmpty(historyData.getProvince())) {
            historyData.setProvince("");
        }
        if (TextUtils.isEmpty(historyData.getCity())) {
            historyData.setCity("");
        }
        return historyData.getRealName().equals(nicknameTV.getText().toString().trim()) &&
                historyData.getSex() == sex &&
                historyData.getDescribe().equals(signateTV.getText().toString().trim()) &&
                historyData.getIndustry().equals(industryTV.getText().toString().trim()) &&
                historyData.getPost().equals(jobsTV.getText().toString().trim()) &&
                historyData.getPlatform().equals(platformTV.getText().toString().trim()) &&
                historyData.getProvince().equals(province) &&
                historyData.getCity().equals(city) && !isHeadPicChange;

    }

    @Override
    public void onBackPressed() {
        if (null != dialog && dialog.isShowing()) {
            dialog.dismiss();
        } else if (null != pvOptions && pvOptions.isShowing()) {
            pvOptions.dismiss();
        } else {
            super.onBackPressed();
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void showLoadingDialog(){
        pDialog = LoadingWaitDialog.createLoadingDialog(this, "");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    private void cancelLoadingDialog(){
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

}
